package com.stropheware.pathfinding;

import java.util.ArrayList;
import com.stropheware.droidDefense.AbstractTower;
import com.stropheware.droidDefense.Cell;


/*
 * Represents a "connected" set of towers
 */

public class TowerSet {
	
	public boolean hasTopEdge, hasBottomEdge;
	
	private static long counter = 0;
	
	private long towerSetNum;
	
	public ArrayList<AbstractTower> towerList;
	
	public TowerSet() {
		hasTopEdge = hasBottomEdge = false;
		towerList = new ArrayList<AbstractTower>();
		towerSetNum = counter;
		if(counter == Long.MAX_VALUE - 2) counter = 0;
		else counter++;
	}
	
	public boolean contains(AbstractTower inQuestion) {
		return towerList.contains(inQuestion);
	}
	
	private void evaluateEdgeBooleansOverall() {
		hasTopEdge = hasBottomEdge = false;
		for(int i=0; i < towerList.size(); i++) {
			evaluateEdgeBooleans(towerList.get(i));
		}
	}
	
	private void evaluateEdgeBooleans(AbstractTower addTower) {
		if(!hasTopEdge && !hasBottomEdge) {
			Cell[][] occupiedCells = addTower.cellsOccupied;
			for(int i=0; i < AbstractTower.TOWER_SIDE_LENGTH; i++) {
				for(int y=0; y < AbstractTower.TOWER_SIDE_LENGTH; y++) {
					if (occupiedCells[i][y].isTopEdge) hasTopEdge = true;
					if (occupiedCells[i][y].isBottomEdge) hasBottomEdge = true;
				}
			}
		}
	}
	
	public void addTowerSet(TowerSet addTowerSet) {
		for(int i=0; i< addTowerSet.towerList.size(); i++) {
			addTowerSet.towerList.get(i).currentTowerSet = this;
			addTower(addTowerSet.towerList.get(i));
		}
	}
	
	public void addTower(AbstractTower addTower) {
		towerList.add(addTower);
		addTower.currentTowerSet = this;
		evaluateEdgeBooleans(addTower);
	}
	
	@Override
	public boolean equals(Object o) {
		return (o instanceof TowerSet && this.towerSetNum == ((TowerSet)o).towerSetNum);
	}
}
